﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Widgets
{
    /// <summary>
    /// VirtualKeyboard.xaml 的交互逻辑
    /// </summary>
    public partial class VirtualKeyboard : UserControl
    {
        public VirtualKeyboard()
        {
            InitializeComponent();
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn == null) return;

            var keyText = btn.Tag as string;
            if (string.IsNullOrEmpty(keyText)) return;

            var isShift = btnLeftShift.IsChecked == true || btnRightShift.IsChecked == true;
            var isCtrl = btnLeftCtrl.IsChecked == true || btnRightCtrl.IsChecked == true;
            var isAlt = btnLeftAlt.IsChecked == true || btnRightAlt.IsChecked == true;

            if (isShift)
            {
                if (btnCapsLock.IsChecked != true)
                {
                    if (keyText.Length == 1)
                    {
                        if ((keyText[0] >= 'a' && keyText[0] <= 'z'))
                        {
                            keyText = keyText.ToUpper();
                        }
                        else if ("`1234567890-=[]\\;',./\t".Contains(keyText[0]))
                        {
                            keyText = ConvertToUpCase(keyText);
                        }
                    }
                }
                else
                {
                    if (keyText.Length == 1 && (keyText[0] >= 'a' && keyText[0] <= 'z'))
                    {
                        keyText = keyText.ToLower();
                    }
                }
            }
            else
            {
                if (btnCapsLock.IsChecked != true)
                {
                    if (keyText.Length == 1)
                    {
                        if ((keyText[0] >= 'a' && keyText[0] <= 'z'))
                            keyText = keyText.ToLower();
                    }
                }
                else
                {
                    if (keyText.Length == 1)
                    {
                        if ((keyText[0] >= 'a' && keyText[0] <= 'z'))
                        {
                            keyText = keyText.ToUpper();
                        }
                        else if ("`1234567890-=[]\\;',./\t".Contains(keyText[0]))
                        {
                            keyText = ConvertToUpCase(keyText);
                        }
                    }
                }
            }

            OnKeyPressed(this, new KeyPressedEventArgs(keyText, isCtrl, isShift, isAlt));

            if (isShift)
            {
                btnLeftShift.IsChecked = false;
                btnRightShift.IsChecked = false;
            }

            if (isCtrl)
            {
                btnLeftCtrl.IsChecked = false;
                btnRightCtrl.IsChecked = false;
            }

            if (isAlt)
            {
                btnLeftAlt.IsChecked = false;
                btnRightAlt.IsChecked = false;
            }

            var itext = keyText.ToLower();

            if (itext == "backspace")
            {
                if (tbKeyword.SelectedText.Length > 0)
                {
                    tbKeyword.SelectedText = "";
                }
                else
                {
                    if (tbKeyword.SelectionStart > 0)
                    {
                        tbKeyword.Select(tbKeyword.SelectionStart - 1, 1);
                        tbKeyword.SelectedText = "";
                    }
                }
            }
            else if (itext == "clear")
            {
                tbKeyword.Text = "";
            }
            else if (itext == "del")
            {
                if (tbKeyword.SelectedText.Length > 0)
                {
                    tbKeyword.SelectedText = "";
                }
                else
                {
                    if (tbKeyword.SelectionStart < tbKeyword.Text.Length - 1)
                    {
                        tbKeyword.Select(tbKeyword.SelectionStart + 1, 1);
                        tbKeyword.SelectedText = "";
                    }
                }
            }
            else if (itext.Length == 1)
            {
                var c = itext[0];
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
                {
                    tbKeyword.SelectedText = itext;
                    tbKeyword.Select(tbKeyword.SelectionStart + 1, 0);
                }
            }
        }

        /// <summary>
        /// 在虚拟键盘中输入的文本。
        /// </summary>
        public string Text { get { return tbKeyword.Text; } }

        public void ClearText()
        {
            tbKeyword.Text = "";
        }

        private string ConvertToUpCase(string s)
        {
            switch (s)
            {
                case "`": return "~";
                case "1": return "!";
                case "2": return "@";
                case "3": return "#";
                case "4": return "$";
                case "5": return "%";
                case "6": return "^";
                case "7": return "&";
                case "8": return "*";
                case "9": return "(";
                case "0": return ")";
                case "-": return "_";
                case "=": return "+";
                case "[": return "{";
                case "]": return "}";
                case "\\": return "|";
                case ";": return ":";
                case "'": return "\"";
                case ",": return "<";
                case ".": return ">";
                case "/": return "?";
                default: return s;
            }
        }

        public event EventHandler<KeyPressedEventArgs> KeyPressed;

        protected void OnKeyPressed(object sender, KeyPressedEventArgs e)
        {
            KeyPressed?.Invoke(sender, e);
        }

        private void BtnLeftShift_Checked(object sender, RoutedEventArgs e)
        {
            btnLeftShift.Background = Brushes.SaddleBrown;
        }

        private void BtnLeftShift_Unchecked(object sender, RoutedEventArgs e)
        {
            btnLeftShift.Background = Brushes.White;
        }

        private void BtnRightShift_Checked(object sender, RoutedEventArgs e)
        {
            btnRightShift.Background = Brushes.SaddleBrown;
        }

        private void BtnRightShift_Unchecked(object sender, RoutedEventArgs e)
        {
            btnRightShift.Background = Brushes.White;
        }

        private void BtnLeftCtrl_Checked(object sender, RoutedEventArgs e)
        {
            btnLeftCtrl.Background = Brushes.SaddleBrown;
        }

        private void BtnLeftCtrl_Unchecked(object sender, RoutedEventArgs e)
        {
            btnLeftCtrl.Background = Brushes.White;
        }

        private void BtnLeftAlt_Checked(object sender, RoutedEventArgs e)
        {
            btnLeftAlt.Background = Brushes.SaddleBrown;
        }

        private void BtnLeftAlt_Unchecked(object sender, RoutedEventArgs e)
        {
            btnLeftAlt.Background = Brushes.White;
        }

        private void BtnRightAlt_Checked(object sender, RoutedEventArgs e)
        {
            btnRightAlt.Background = Brushes.SaddleBrown;
        }

        private void BtnRightAlt_Unchecked(object sender, RoutedEventArgs e)
        {
            btnRightAlt.Background = Brushes.White;
        }

        private void BtnRightCtrl_Checked(object sender, RoutedEventArgs e)
        {
            btnRightCtrl.Background = Brushes.SaddleBrown;
        }

        private void BtnRightCtrl_Unchecked(object sender, RoutedEventArgs e)
        {
            btnRightCtrl.Background = Brushes.White;
        }

        private void BtnCapsLock_Checked(object sender, RoutedEventArgs e)
        {
            btnCapsLock.Background = Brushes.SaddleBrown;
        }

        private void BtnCapsLock_Unchecked(object sender, RoutedEventArgs e)
        {
            btnCapsLock.Background = Brushes.White;
        }

        private void BtnLittle_Checked(object sender, RoutedEventArgs e)
        {
            btnLittle.Background = Brushes.SaddleBrown;
            btnMiddle.IsChecked = false;
            btnBig.IsChecked = false;

            App.ConfigManager.Set("VirtualKeyboardSize", "Little");

            if (this.Parent != null)
            {
                var parentBorder = this.Parent as Border;
                if (parentBorder != null && parentBorder.Parent != null)
                {
                    var parentPop = parentBorder.Parent as Popup;
                    if (parentPop != null)
                    {
                        parentPop.Width = 500;
                        parentPop.Height = 222;
                    }
                }
            }

            var isShift = btnLeftShift.IsChecked == true || btnRightShift.IsChecked == true;
            var isCtrl = btnLeftCtrl.IsChecked == true || btnRightCtrl.IsChecked == true;
            var isAlt = btnLeftAlt.IsChecked == true || btnRightAlt.IsChecked == true;

            OnKeyPressed(this, new KeyPressedEventArgs("SizeChanged-Little", isCtrl, isShift, isAlt));
        }

        private void BtnLittle_Unchecked(object sender, RoutedEventArgs e)
        {
            btnLittle.Background = Brushes.White;
        }

        private void BtnMiddle_Checked(object sender, RoutedEventArgs e)
        {
            btnMiddle.Background = Brushes.SaddleBrown;
            btnLittle.IsChecked = false;
            btnBig.IsChecked = false;

            App.ConfigManager.Set("VirtualKeyboardSize", "Middle");

            var parentBorder = this.Parent as Border;
            if (parentBorder != null && parentBorder.Parent != null)
            {
                var parentPop = parentBorder.Parent as Popup;
                if (parentPop != null)
                {
                    parentPop.Width = 722;
                    parentPop.Height = 272;
                }
            }

            var isShift = btnLeftShift.IsChecked == true || btnRightShift.IsChecked == true;
            var isCtrl = btnLeftCtrl.IsChecked == true || btnRightCtrl.IsChecked == true;
            var isAlt = btnLeftAlt.IsChecked == true || btnRightAlt.IsChecked == true;

            OnKeyPressed(this, new KeyPressedEventArgs("SizeChanged-Middle", isCtrl, isShift, isAlt));
        }

        private void BtnMiddle_Unchecked(object sender, RoutedEventArgs e)
        {
            btnMiddle.Background = Brushes.White;
        }

        private void BtnBig_Checked(object sender, RoutedEventArgs e)
        {
            btnBig.Background = Brushes.SaddleBrown;
            btnLittle.IsChecked = false;
            btnMiddle.IsChecked = false;

            App.ConfigManager.Set("VirtualKeyboardSize", "Big");

            if (this.Parent != null)
            {
                var parentBorder = this.Parent as Border;
                if (parentBorder != null && parentBorder.Parent != null)
                {
                    var parentPop = parentBorder.Parent as Popup;
                    if (parentPop != null)
                    {
                        parentPop.Width = 962;
                        parentPop.Height = 362;
                    }
                }
            }

            var isShift = btnLeftShift.IsChecked == true || btnRightShift.IsChecked == true;
            var isCtrl = btnLeftCtrl.IsChecked == true || btnRightCtrl.IsChecked == true;
            var isAlt = btnLeftAlt.IsChecked == true || btnRightAlt.IsChecked == true;

            OnKeyPressed(this, new KeyPressedEventArgs("SizeChanged-Big", isCtrl, isShift, isAlt));
        }

        private void BtnBig_Unchecked(object sender, RoutedEventArgs e)
        {
            btnBig.Background = Brushes.White;
        }
    }

    public class KeyPressedEventArgs : RoutedEventArgs
    {
        public KeyPressedEventArgs(string keyText, bool isCtrl = false, bool isShift = false, bool isalt = false)
        {
            KeyText = keyText;
            IsCtrl = isCtrl;
            IsShift = isShift;
            IsAlt = isalt;
        }

        public string KeyText { get; private set; }
        public bool IsCtrl { get; private set; }
        public bool IsShift { get; private set; }
        public bool IsAlt { get; private set; }
    }
}
